<template>
    <div class="detail-page">
        <van-nav-bar
            left-text="返回"
            @click-left="$router.back()"
            fixed
            title="面经详情"
        />
        <header class="header">
            <h1>{{ detail.stem }}</h1>
            <p>
                {{ detail.createdAt }} | {{ detail.views }} 浏览量 | {{ detail.likeCount }} 点赞数
            </p>
            <p>
                <img :src="detail.avatar" alt=""/>
                <span>{{ detail.creator }}</span>
            </p>
        </header>
        <main class="body" v-html="detail.content">
        </main>
        <div class="opt">
            <van-icon @click="activeArticle(1)" :class="detail.likeFlag ? 'active' : ''" name="like-o"/>
            <van-icon @click="activeArticle(2)" :class="detail.collectFlag ? 'active' : ''" name="star-o"/>
        </div>
    </div>
</template>

<script>
import { getDetail, setArticleFlag } from '@/api/article'

export default {
    name: 'detail-page',
    data () {
        return {
            detail: {}
        }
    },
    async created () {
        const res = await getDetail({id: this.$route.params.id})
        this.detail = res.data.data
        console.log(res.data.data)
    },
    methods: {
        async activeArticle(i) {
            if (i === 1) {
                this.detail.likeFlag ? this.detail.likeFlag = 0 : this.detail.likeFlag = 1
            } else {
                this.detail.collectFlag ? this.detail.collectFlag = 0 : this.detail.collectFlag = 1
            }
            const res = await setArticleFlag({id: this.detail.id, optType: i})
        },
    }
}
</script>

<style lang="less" scoped>
.detail-page {
    margin-top: 44px;
    overflow: hidden;
    padding: 0 15px;

    .header {
        h1 {
            font-size: 24px;
        }

        p {
            color: #999999;
            font-size: 12px;
            display: flex;
            align-items: center;
        }

        img {
            width: 40px;
            height: 40px;
            border-radius: 50%;
            overflow: hidden;
        }
    }

    .opt {
        position: fixed;
        bottom: 100px;
        right: 0;

        > .van-icon {
            margin-right: 20px;
            background: #FFFFFF;
            width: 40px;
            height: 40px;
            line-height: 40px;
            text-align: center;
            border-radius: 50%;
            box-shadow: 2px 2px 10px #CCCCCC;
            font-size: 18px;

            &.active {
                background: #FEC635;
                color: #FFFFFF;
            }
        }
    }
}
</style>
